package com.frejoys.service.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.poi.excel.ExcelUtil;
import cn.hutool.poi.excel.ExcelWriter;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.frejoys.common.dto.admin.StoreCodeAndNameDto;
import com.frejoys.common.dto.admin.StoreListDto;
import com.frejoys.common.dto.common.PageDto;
import com.frejoys.common.enums.EFileSize;
import com.frejoys.common.enums.EStoreCodType;
import com.frejoys.common.enums.EStoreCodeDepositoryStatus;
import com.frejoys.common.enums.EStoreCodeStatus;
import com.frejoys.common.service.AliStorageService;
import com.frejoys.common.util.AppConfigUtil;
import com.frejoys.common.vo.admin.StoreAndCodeVo;
import com.frejoys.common.vo.admin.StoreCodeDepositoryVo;
import com.frejoys.dao.entity.Store;
import com.frejoys.dao.entity.StoreCode;
import com.frejoys.dao.entity.StoreCodeBatch;
import com.frejoys.dao.entity.StoreCodeDepository;
import com.frejoys.dao.mapper.StoreCodeBatchMapper;
import com.frejoys.dao.mapper.StoreCodeDepositoryMapper;
import com.frejoys.dao.mapper.StoreCodeMapper;
import com.frejoys.dao.mapper.StoreMapper;
import com.frejoys.service.CommonService;
import com.frejoys.service.StoreCodeBatchService;
import com.frejoys.service.StoreCodeDepositoryService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
@RequiredArgsConstructor
public class StoreCodeDepositoryServiceImpl extends ServiceImpl<StoreCodeDepositoryMapper, StoreCodeDepository> implements StoreCodeDepositoryService {

    private final AliStorageService aliStorageService;

    private final StoreCodeBatchMapper storeCodeBatchMapper;

    private final CommonService commonService;

    private final StoreCodeMapper storeCodeMapper;

    private final StoreMapper storeMapper;


    @Override
    public IPage<StoreCodeDepositoryVo> storeCodeDepositoryListInfo(StoreCodeAndNameDto dto) {
        Page page = new Page<>(dto.getPage(), dto.getSize());

        List<Long> codeList = new ArrayList<>();
        if (StrUtil.isNotEmpty(dto.getName())){
            List<Store> stores = storeMapper.getStoreNameById(dto.getName());
            List<Integer> storeIdList = stores.stream().map(Store::getId).toList();
            if (storeIdList.isEmpty()){
                return new Page<>();
            }
            LambdaQueryWrapper<StoreCode> storeCodeWrapper = new LambdaQueryWrapper<>();
            storeCodeWrapper.select(StoreCode::getCode)
                    .eq(StoreCode::getType,EStoreCodType.offLine.getValue())
                    .in(StoreCode::getStoreId,storeIdList);
            List<StoreCode> storeCodes = storeCodeMapper.selectList(storeCodeWrapper);
            codeList = storeCodes.stream().map(StoreCode::getCode).toList();

        }
        //查询店铺支付码
        IPage<StoreCodeDepositoryVo> storeCodeDepositoryPage = baseMapper.storeCodeDepositoryListInfo(page,codeList,dto,EStoreCodeDepositoryStatus.await.getValue());
        List<StoreCodeDepositoryVo> list = storeCodeDepositoryPage.getRecords();

        if (!list.isEmpty()){
            List<Long> codes = list.stream().map(StoreCodeDepositoryVo::getCode).toList();

            Map<Integer, Store> storeMap = new HashMap<>();

            LambdaQueryWrapper<StoreCode> wrapper = new LambdaQueryWrapper<>();
            wrapper.select(StoreCode::getCode,StoreCode::getStoreId)
                    .eq(StoreCode::getType, EStoreCodType.offLine.getValue())
                    .in(StoreCode::getCode,codes);
            List<StoreCode> storeCodes = storeCodeMapper.selectList(wrapper);
            List<Integer> storeIds = storeCodes.stream().map(StoreCode::getStoreId).toList();
            Map<Long, Integer> storeIdMap = storeCodes.stream().collect(Collectors.toMap(StoreCode::getCode, StoreCode::getStoreId));

            if (!storeIds.isEmpty()){
                    //查询店铺信息
                LambdaQueryWrapper<Store> storeWrapper = new LambdaQueryWrapper<>();
                storeWrapper.select(Store::getId,Store::getName,Store::getLogo)
                        .in(Store::getId,storeIds);
                List<Store> storeList = storeMapper.selectList(storeWrapper);
                storeMap = storeList.stream().collect(Collectors.toMap(Store::getId, store -> store));
            }

            for (StoreCodeDepositoryVo vo : list) {

                if (storeIdMap.containsKey(vo.getCode())){
                    Store store = storeMap.get(storeIdMap.get(vo.getCode()));
                    if (ObjectUtil.isNotEmpty(store)){
                        vo.setStoreId(store.getId());
                        vo.setStoreName(store.getName());
                        vo.setLogoUrl(AppConfigUtil.getUrlPrefix(store.getLogo(), EFileSize.logo.getValue()));
                    }
                }

                vo.setPayCode(aliStorageService.createQrCodeLogoPayImage(String.valueOf(vo.getCode())));
            }
        }
        return storeCodeDepositoryPage;
    }

}
